package com.yulongtian.interviewWorks.day20221114;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * https://leetcode.cn/problems/find-closest-lcci/?favorite=xb9lfcwi
 * 可
 *
 * @author yulongTian
 * @create 2022-11-14 10:26
 */
public class Test01 {
    public static void main(String[] args) {

    }

    public int findClosest(String[] words, String word1, String word2) {

        HashMap<String, List<Integer>> map = new HashMap<>();

        for (int i = 0; i < words.length; i++) {

            //和两个都不相等
            if (!words[i].equals(word1) && !words[i].equals(word2)) {
                continue;
            }

            //不包含
            if (!map.containsKey(words[i])) {
                List<Integer> temp = new ArrayList<>();
                temp.add(i);
                map.put(words[i], temp);
            } else {
                List<Integer> list = map.get(words[i]);
                list.add(i);
                map.put(words[i], list);
            }
        }

        List<Integer> word1List = map.get(word1);
        List<Integer> word2List = map.get(word2);

        int res = Integer.MAX_VALUE;

        for (Integer integer : word1List) {
            for (Integer integer1 : word2List) {
                res = Math.min(res, Math.abs(integer - integer1));
            }
        }


        return res;
    }

}
